﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using WealowsHospital;
namespace WealowsHospital.BLL
{
    /// <summary>
    /// lop staffBus thuc hien cac business lien quan den staff
    /// </summary>
    class StaffBus
    {

        private WellmeadowsEntities hospital;
        private List<Staff> staffs;
        #region staff

        public StaffBus(WellmeadowsEntities hospital)
        {
            this.hospital = hospital;
        }
        /// <summary>
        /// Khoi tao Staff
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="address"></param>
        /// <param name="gendre"></param>
        /// <param name="dateOfBirth"></param>
        /// <param name="insuranceNumber"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="position"></param>
        /// <param name="currentSalary"></param>
        /// <param name="workHoursPerWeek"></param>
        /// <param name="salaryScale"></param>
        /// <param name="salaryPayment"></param>
        /// <param name="password"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public Staff CreateStaff(
            string staffId,
            string firstName,
            string lastName,
            string address,
            string gendre,
            DateTime dateOfBirth,
            string insuranceNumber,
            string phoneNumber,
            decimal currentSalary,
            int workHoursPerWeek,
            string salaryScale,
            string salaryPayment,
            string password,
            int roleId)
        {
            Staff staff = new Staff()
            {
                StaffID=staffId,
                FirstName=firstName,
                LastName=lastName,
                Address=address,
                Gender=gendre,
                DateOfBirth=dateOfBirth,
                InsuranceNumber=insuranceNumber,
                PhoneNumber=phoneNumber,
                CurrentSalary=currentSalary,
                WorkHoursPerWeek=workHoursPerWeek,
                SalaryScale=salaryScale,
                SalaryPayment=salaryPayment,
                Password=password,
            };
            hospital.AddToStaff(staff);
            staff.Role = hospital.Role.Where(rol => rol.id == roleId).FirstOrDefault();
            hospital.SaveChanges();
            return staff;
        }
        /// <summary>
        /// xoa staff
        /// </summary>
        /// <param name="staffId">Staff ID</param>
        /// <returns></returns>
        public bool DeleteStaff(string staffId)
        {
            Staff stf = hospital.Staff.Where(stx => stx.StaffID == staffId).FirstOrDefault();
            
            hospital.DeleteObject(stf);
            return hospital.SaveChanges() > 0;
        }

        /// <summary>
        /// xoa staff (van con luu trong csdl)
        /// </summary>
        /// <param name="staffId"></param>
        /// <returns></returns>
        public Boolean TmpDeleteStaff(string staffId)
        {
            Staff stf = hospital.Staff.Where(stx => stx.StaffID == staffId).FirstOrDefault();
            stf.isDeleted = 1;
            return hospital.SaveChanges() > 0;
        }

        /// <summary>
        /// cap nhat staff
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="address"></param>
        /// <param name="gendre"></param>
        /// <param name="dateOfBirth"></param>
        /// <param name="insuranceNumber"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="currentSalary"></param>
        /// <param name="workHoursPerWeek"></param>
        /// <param name="salaryScale"></param>
        /// <param name="salaryPayment"></param>
        /// <param name="password"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool UpdateStaff(string staffId,
            string firstName,
            string lastName,
            string address,
            string gendre,
            DateTime dateOfBirth,
            string insuranceNumber,
            string phoneNumber,
            decimal currentSalary,
            int workHoursPerWeek,
            string salaryScale,
            string salaryPayment,
            string password,
            int roleId)
        {
            Staff stf = hospital.Staff.Where(stx => stx.StaffID == staffId).FirstOrDefault();
            
            stf.FirstName = firstName;
            stf.LastName = lastName;
            stf.Address = address;
            stf.Gender = gendre;
            stf.DateOfBirth = dateOfBirth;
            stf.InsuranceNumber = insuranceNumber;
            stf.PhoneNumber = phoneNumber;
            stf.Role = hospital.Role.Where(rol => rol.id == roleId).FirstOrDefault();
            stf.CurrentSalary = currentSalary;
            stf.WorkHoursPerWeek = workHoursPerWeek;
            stf.SalaryScale = salaryScale;
            stf.SalaryPayment = salaryPayment;
            stf.Password = password;
            
            return hospital.SaveChanges() > 0;

        }

        #endregion staff

        #region epxerience
        /// <summary>
       /// Add Experience for user
       /// </summary>
       /// <param name="staff">stff</param>
       /// <param name="startDate">date that start working</param>
       /// <param name="endDate">date that stop working</param>
       /// <param name="organization">organization</param>
       /// <returns></returns>

        public Experience AddExperience(
            Staff staff,
            DateTime startDate,
            DateTime endDate,
            String organization)
        {
            Experience experience = new Experience()
            {
                StartDate=startDate,
                EndDate=endDate,
                Orangnization=organization,
                Staff=staff
            };
            try
            {
                hospital.AddToExperience(experience);
                hospital.SaveChanges();
            }
            catch (Exception ex)
            {
                return null;
            }
            return experience;
        }

        //xoa experience
        public bool DeleteExperience(int experienceId)
        {
            Experience exper = hospital.Experience.Where(exp => exp.expId == experienceId).FirstOrDefault();
            hospital.DeleteObject(exper);
            return hospital.SaveChanges() > 0;
        }
        //cap nhat experiece 
        public bool UpdateExperience(int experienceId, DateTime startDate, DateTime endDate, string organization)
        {
            Experience exper = hospital.Experience.Where(exp => exp.expId == experienceId).FirstOrDefault();
            exper.StartDate = startDate;
            exper.EndDate = endDate;
            exper.Orangnization = organization;
            return hospital.SaveChanges() > 0;
        }
        #endregion experience
        #region role
        /// <summary>
        /// Add user Role
        /// </summary>
        /// <param name="roleId">Role ID</param>
        /// <param name="roleName">Name Of Role</param>
        public bool AddRole(string roleName)
        {
            Role role = new Role()
            {
                name=roleName
            };
            hospital.AddToRole(role);
            return hospital.SaveChanges() > 0;
        }
        #endregion role
        #region qualification
        /// <summary>
        /// Add Qualification To Staff
        /// </summary>
        /// <param name="staff"></param>
        /// <param name="graduationDate"></param>
        /// <param name="institution"></param>
        /// <param name="typeOfEducation"></param>
        /// <returns></returns>
        public bool AddQualification(Staff staff,DateTime graduationDate,string institution,string typeOfEducation)
        {
            Qualifications qualification = new Qualifications() {
                GranduationDate=graduationDate,
                Institution=institution,
                TypeOfEducation=typeOfEducation,
                Staff=staff
            };
            hospital.AddToQualifications(qualification);

            return hospital.SaveChanges() > 0;
        }
        /// <summary>
        /// xoa qualification
        /// </summary>
        /// <param name="qualificationId"></param>
        /// <returns></returns>
        public bool DeleteQualification(int qualificationId)
        {
            int i=0;
            Qualifications qualification=null;
            try
            {
                qualification = hospital.Qualifications.Where(quad => quad.QuaID == qualificationId).FirstOrDefault();
                hospital.DeleteObject(qualification);
                i = hospital.SaveChanges();
            }
            catch (Exception ex)
            {
                hospital.Refresh(System.Data.Objects.RefreshMode.StoreWins, qualification);
                i = hospital.SaveChanges();
            }
            return i > 0;
        }
        //cap nhat qualification
        public bool UpdateQualification(int qualificationId, DateTime graduationDate, String institution, string typeOfEducation)
        {
            Qualifications qualification = hospital.Qualifications.Where(quad => quad.QuaID == qualificationId).First();
            qualification.GranduationDate=graduationDate;
            qualification.Institution = institution;
            qualification.TypeOfEducation = typeOfEducation;
            return hospital.SaveChanges()>0;
           
        }
        #endregion qualification
    }
}
